from pydub import AudioSegment, silence

import os


def listdir(path, list_name):  # 传入存储的list
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            listdir(file_path, list_name)
        else:
            if file_path.endswith(".wav"):
                list_name.append(file_path.replace("\\", "/"))


if __name__ == '__main__':
    wan_root_path = "wav"
    wav_list = []
    min_length = 3000  # 截取出的音频长度不会低于这个值，单位：ms
    min_silence_len = 400  # 当语音间隔长度大于这个值时进行一次截断，单位：ms
    min_to_cut = 6000  # 如果原音频短于这个长度则不会被切割，单位：ms
    listdir(wan_root_path + "/input", wav_list)
    print(f"读取到的文件列表：{wav_list}")
    for wav_path in wav_list:
        wav_name = wav_path.split("/")[-1:][0].split(".")[0]  # 从路径中截取出不带后缀的文件名
        sound = AudioSegment.from_wav(wav_path)
        if sound.duration_seconds * 1000 < min_to_cut:
            sound.export(wan_root_path + "/output/" + wav_name + ".wav", format="wav")
            continue
        sound_cut_list = silence.split_on_silence(sound, min_silence_len=min_silence_len, silence_thresh=-50,
                                                  keep_silence=200)
        print(f"读取文件{wav_path}")
        print(f"截取到{len(sound_cut_list)}个！")
        cut_file = AudioSegment.silent(1)  # 占位
        cut_file_backup = AudioSegment.silent(1)  # 保存最后被写入文件的声音文本，以防出现在最后切出一段很短的音频，如果出现这种情况，将其合入前一个音频文件
        cut_file_length = 0
        i = 0
        for sound_cut in sound_cut_list:
            if cut_file.duration_seconds <= 0.001:
                cut_file = sound_cut
            else:
                cut_file = cut_file + AudioSegment.silent(min_silence_len) + sound_cut
            if cut_file.duration_seconds * 1000 > min_length:  # 检查如果截取到的音频过短，则将其累积到下一条
                # print("---------")
                # print(sound_cut.duration_seconds)
                # print(cut_file.duration_seconds)
                cut_file.export(wan_root_path + "/output/" + wav_name + "_" + str(i) + ".wav", format="wav")
                cut_file_backup = cut_file
                print(f"写入！为：{cut_file.duration_seconds}秒")
                print(f"此时备份长度为：{cut_file_backup.duration_seconds}秒")
                i = i + 1
                cut_file = AudioSegment.silent(1)
            else:
                print(f"过短！为：{cut_file.duration_seconds}秒,累计")
        if cut_file.duration_seconds >= 0.001:
            print(f"需要覆盖，备份的长度为：{cut_file_backup.duration_seconds}秒")
            cut_file = cut_file_backup + AudioSegment.silent(min_silence_len) + cut_file
            cut_file.export(wan_root_path + "/output/" + wav_name + "_" + str(i-1) + ".wav", format="wav")
            print(f"加长后覆盖上一条！为：{cut_file.duration_seconds}秒")


            # cut_file.export(wan_root_path + "/output/" + wav_name + "_" + str(i) + ".wav", format="wav")
            # i = i + 1
            # cut_file = AudioSegment.silent(1)
        print(f"截取出{i}个音频文件")
